#!/bin/bash

# 启用错误检查
set -e

# 定义颜色代码
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m' # 重置颜色

# 函数：输出步骤状态
log_step() {
    local step_name=$1
    local status=$2
    if [ "$status" -eq 0 ]; then
        echo -e "${GREEN}[SUCCESS]${NC} Step: $step_name"
    else
        echo -e "${RED}[ERROR]${NC} Step: $step_name failed with exit code $status"
        exit $status
    fi
}

# 获取脚本所在目录的绝对路径
SCRIPT_DIR=$(dirname "$(realpath "$0")")

# # 步骤 0: 检查并创建 /opt/sherpa-onnx 目录
# echo "Checking /opt/sherpa-onnx directory..."
# if [ ! -d "/opt/sherpa-onnx" ]; then
#     echo "Directory /opt/sherpa-onnx does not exist. Creating..."
#     sudo mkdir -p /opt/sherpa-onnx
#     log_step "Create /opt/sherpa-onnx" $?
# else
#     echo "Directory /opt/sherpa-onnx already exists."
#     log_step "Check /opt/sherpa-onnx" 0
# fi

# # 步骤 1: 清空 /opt/sherpa-onnx 目录
# echo "Clearing /opt/sherpa-onnx directory..."
# sudo rm -rf /opt/sherpa-onnx/*
# log_step "Clear /opt/sherpa-onnx" $?

# # 步骤 2: 复制 install/* 文件到 /opt/sherpa-onnx
# INSTALL_DIR="$SCRIPT_DIR/../install"  # 相对于脚本文件的路径
# echo "Copying files from $INSTALL_DIR to /opt/sherpa-onnx..."
# if [ -d "$INSTALL_DIR" ]; then
#     sudo cp -r "$INSTALL_DIR"/* /opt/sherpa-onnx/
#     log_step "Copy files to /opt/sherpa-onnx" $?
# else
#     echo -e "${RED}[ERROR]${NC} Directory $INSTALL_DIR does not exist."
#     exit 1
# fi

# # 删掉所有编译文件
# echo "Checking if 'build' directory exists..."
# if [ -d "build" ]; then
#     echo "'build' directory exists. Deleting its contents..."
#     rm -rf build/*
#     log_step "Delete all files in 'build/*'" $?
# else
#     echo "'build' directory does not exist. Skipping..."
#     log_step "Check 'build' directory" 0
# fi

# 步骤 3: 检查并安装 libcurl4-openssl-dev
echo "Checking if libcurl4-openssl-dev is installed..."
if ! dpkg -l | grep -q libcurl4-openssl-dev; then
    echo "Installing libcurl4-openssl-dev..."
    sudo apt install -y libcurl4-openssl-dev
    log_step "Install libcurl4-openssl-dev" $?
else
    echo "libcurl4-openssl-dev is already installed."
    log_step "Check libcurl4-openssl-dev" 0
fi

# 步骤 4: 检查并安装 libicu-dev
echo "Checking if libicu-dev is installed..."
if ! dpkg -l | grep -q libicu-dev; then
    echo "Installing libicu-dev..."
    sudo apt install -y libicu-dev
    log_step "Install libicu-dev" $?
else
    echo "libicu-dev is already installed."
    log_step "Check libicu-dev" 0
fi

# 步骤 5: 检查并安装 portaudio19-dev
echo "Checking if portaudio19-dev is installed..."
if ! dpkg -l | grep -q portaudio19-dev; then
    echo "Installing portaudio19-dev..."
    sudo apt install -y portaudio19-dev
    log_step "Install portaudio19-dev" $?
else
    echo "portaudio19-dev is already installed."
    log_step "Check portaudio19-dev" 0
fi


# nlohmann-json3-dev 用于 c++ json 解析
echo "Checking if nlohmann-json3-dev is installed..."
if ! dpkg -l | grep -q nlohmann-json3-dev; then
    echo "Installing nlohmann-json3-dev..."
    sudo apt install -y nlohmann-json3-dev
    log_step "Install nlohmann-json3-dev" $?
else
    echo "nlohmann-json3-dev is already installed."
    log_step "Check nlohmann-json3-dev" 0
fi

# SpeexDSP 对音频进行进行软件重采样 和 降噪
sudo apt install libspeexdsp-dev

# 將生成的低採樣率的語音 轉爲 高採樣率的語音
sudo apt install libsamplerate0-dev




# 所有步骤完成
echo -e "${GREEN}All steps completed successfully!${NC}"